#include "pch.h"
#include "ImgPreHandle.h"
#include <string>
#include <opencv2/opencv.hpp>

char* STDCALL sayHelloWorld(const char* hello)
{
    int helloLen = std::strlen(hello);
    char* say = new char[helloLen + 6];
    std::memset(say, 0, sizeof(char) * (helloLen + 6));
    memcpy(say, hello, helloLen);
    memcpy(say + helloLen, "World", sizeof(char) * 5);

    return say;
}

cv::Mat matFromBytes(byte* srcMat, int srcMatW, int srcMatH, int bytePerPixel)
{
    if (bytePerPixel == 1)
    {
        return cv::Mat(srcMatH, srcMatW, CV_8UC1, srcMat);
    }
    else if (bytePerPixel == 3)
    {
        return cv::Mat(srcMatH, srcMatW, CV_8UC3, srcMat);
    }
    else
    {
        return cv::Mat();
    }
}

void STDCALL cvBlur(byte* srcMat, int srcMatW, int srcMatH, int bytePerPixel, byte*& dstMat, int& dstMatW, int& dstMatH, int convSize)
{
    cv::Mat sMat = matFromBytes(srcMat, srcMatW, srcMatH, bytePerPixel);
    if (sMat.empty())
    {
        dstMat = nullptr;
        dstMatW = 0;
        dstMatH = 0;
    }

    cv::Mat dMat;
    cv::blur(sMat, dMat, cv::Size(convSize, convSize));

    dstMatW = srcMatW;
    dstMatH = srcMatH;
    dstMat = new byte[dMat.total() * dMat.elemSize()];
    memcpy(dstMat, dMat.ptr(), dMat.step * dMat.rows);
    return;
}
